package com.b2mining.integration.helper;


import java.io.*;

import org.apache.log4j.Logger;


/**
 * Clase que permite almacena funciones de ayuda para el manejo de archivos
 * @since (7/3/2003 10:35:26 AM)
 * @author Synopsis S.A. <BR>
 * Analista Programador: Janyn Roca
 * 						
 */
public class HelperFile {
    /**
     * Locale specific strings displayed to the user.
     */
//    protected static ResourceBundle labels = ResourceBundle.getBundle("FileHelper",  Locale.getDefault());
    /**
     * Buffer size when reading from input stream.
     */
    private final static int BUFFER_SIZE = 1024;


    /**
     * Move a file from one location to another.  An attempt is made to rename
     * the file and if that fails, the file is copied and the old file deleted.
     *
     * If the destination file already exists, an exception will be thrown.
     *
     * @param from file which should be moved.
     * @param to desired destination of the file.
     * @throws IOException if an error occurs.
     */
    public static void move(File from, File to) throws IOException {
    	Logger log = Logger.getLogger(HelperFile.class);
    	log.debug("**************************** MOVE"+to.toString());
        move(from, to, true);
    }

    /**
     * Move a file from one location to another.  An attempt is made to rename
     * the file and if that fails, the file is copied and the old file deleted.
     *
     * @param from file which should be moved.
     * @param to desired destination of the file.
     * @param overwrite If false, an exception will be thrown rather than overwrite a file.
     * @throws IOException if an error occurs.
     */
    public static void move(File from, File to, boolean overwrite) throws IOException {
    	Logger log = Logger.getLogger(HelperFile.class);
    	log.debug("**************************** ");
        if (to.exists()){
            if (overwrite){
                if (!to.delete()){
                	log.debug("deleteerror:"+to.toString());
                    throw new IOException("deleteerror:"+to.toString());
                }
            } else {
            	log.debug("alreadyexistserror:"+to.toString());
                throw new IOException("alreadyexistserror:"+to.toString());
            }
        }

        if (from.renameTo(to)) return;

        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(from);
            out = new FileOutputStream(to);
            copy(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            if (!from.delete()){
            	log.debug("deleteoriginalerror:"+from.toString());
                throw new IOException("deleteoriginalerror:"+from.toString());

            }
            log.debug("HIZO TODO BIEN");
        } finally {
            if (in != null){
                in.close();
                in = null;
            }
            if (out != null){
                out.flush();
                out.close();
                out = null;
            }
        }
    }


    /**
     * Copy the data from the input stream to the output stream.
     *
     * @param in data source
     * @param out data destination
     * @throws IOException in an input or output error occurs
     */
    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

}
